package main

import (
	"fmt"
	"time"
)

var fibMap = make(map[int]uint64)

func main() {
	start := time.Now()
	for i := 0; i <= 50; i++ {
		fmt.Printf("fibonacci(%d) is: %d\n", i, fibonacci1(i))
	}
	end := time.Now()
	delta := end.Sub(start)
	fmt.Printf("memoization took this amount of time: %s\n", delta)
}

//当在进行大量的计算时，提升性能最直接有效的一种方式就是避免重复计算。通过在内存中缓存和重复利用相同计算的结果，称之为内存缓存

func fibonacci1(n int) (res uint64) {
	if fibMap[n] != 0 {
		res = fibMap[n]
		return
	}
	if n <= 1 {
		res = 1
	} else {
		res = fibonacci1(n-1) + fibonacci1(n-2)
	}
	fibMap[n] = res
	return res
}
